<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/25-10:24
    // +----------------------------------------------------------------------
    // | Description:  部门
    // +----------------------------------------------------------------------


    namespace app\admin\controller\company;


    use app\admin\controller\AdminBase;
    use app\admin\model\Departments;
    use app\admin\validate\DepartmentValidate;
    use Jrk\Trees;

    class Department extends AdminBase
    {

        public function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub

            $this->model=app()->model("Departments");

            $this->del="部门";
        }


        /**
         * @param Departments $departments
         * @return mixed
         * @throws \think\Exception
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/25
         * @name: addDepartment
         * @describe:添加
         */
        public function addDepartment(Departments $departments){
            if (IS_POST){
                $data = $this->request->post();
                $validate=new DepartmentValidate();

                if (!$validate->check($data)){
                    $this->error($validate->getError());
                }

                if (isset($data['id'])){
                    addLog("部门操作","修改部门：".$data['dname']);
                }else{

                    $k=$departments->where("dname",$data['dname'])->where("pid",$data['pid'])->find();

                    if ($k){
                        $this->error("当前部门名称已存在");
                    }

                    addLog("部门操作","新增部门：".$data['dname']);
                }
                return $this->model->addAndEdit($data);
            }

            $tree = Trees::toFormatTree($this->model->deList(),"dname");//获取菜单列表
            $pid = $this->request->param("id/d");//父id

            $this->assign(compact("pid", "tree"));

            $this->assign("info",[]);

            return $this->fetch();
        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/25
         * @name: editDepartment
         * @describe:编辑
         */
        public function editDepartment(){

            $pid = $this->request->param("id/d");//父id

            $info=$this->model->getOne($pid);

            if (!$info){
                $this->assign('msg','当前id:'.$pid.'错误');
                $this->fetch("public/404");
            }

            $tree = Trees::toFormatTree($this->model->deList(),"dname");//获取菜单列表

            $this->assign(compact("pid", "tree"));

            $this->assign("info",$info);
            return $this->fetch("add_department");
        }




    }